package com.ggh.jinjilive.dialog.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemGiftListBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.live.GiftMobile;
import com.ggh.jinjilive.views.GridItemDecoration;

import java.util.List;


public class GiftListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    public GiftListAdapter(Context mContext) {
        this.mContext = mContext;
    }


    private List<GiftMobile> giftMobileList;
    public void GiftData(List<GiftMobile> giftMobileList){
        this.giftMobileList = giftMobileList;
        notifyDataSetChanged();
    }




    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemGiftListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_gift_list, parent, false);
        return new Title(binding);
    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder, int position) {
        Title holder = (Title)recyclerViewholder;
        GiftMobile giftMobile = giftMobileList.get(position);
        if(null != giftMobile){
            holder.binding.tvTitle.setText(giftMobile.getType());
            GiftLiveAdapter1 giftAdapter1 = new GiftLiveAdapter1();
            holder.binding.giftList.setAdapter(giftAdapter1);
            giftAdapter1.GiftData3(giftMobile.getList());
            giftAdapter1.setAddItemOnClickListener(new AddItemOnClickListener() {
                @Override
                public void onSkipClicked(int position1, Object mObject) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,mObject);
                    }
                }
            });
        }

    }



    @Override
    public int getItemCount() {
        return null == giftMobileList ? 0 : giftMobileList.size();
    }


    public class Title extends RecyclerView.ViewHolder {
        ItemGiftListBinding binding;

        public Title(@NonNull ItemGiftListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.giftList.setLayoutManager(new GridLayoutManager(mContext,4));
            binding.giftList.addItemDecoration(new GridItemDecoration(4, SizeUtils.dp2px(10),true));
        }
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }


}
